/*
 *  Copyright 2008-2010 www.jrails.org
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package org.rails.extjs;

import org.rails.extjs.javascript.Script;
import org.rails.extjs.json.Json;

public abstract class AbsBase {
	
	protected StringBuffer script;
	
	public static boolean debug = false;
	
	protected Option option = new Option();

	public abstract String getScript();
	
	public AbsBase() {
		super();
	}
	
	public Option getOption(){
		return (Option)option.clone();
	}

	public void addOption(String name, Object value) {
		option.put(name, value);
	}
	
	public void addAll(Option option){
		option.putAll(option);
	}

	public void removeAttribute(String name) {
		option.remove(name);
	}
	
	public Object getOption(String name) {
		return option.get(name);
	}

	protected void clearOptions() {
		option.clear();
	}

	protected String createOptions() {
		return option.createJson();
	}

	public static String replaceEmpty(Object arg) {
		return arg == null ? "" : arg.toString();
	}
	
	protected String getString(String name, String def) {
		if(getOption(name) == null)
			return def;
		else
			return getOption(name).toString();
	}

	protected Number getNumber(String name, Number def) {
		if(getOption(name) == null)
			return def;
		else
			return (Number)getOption(name);
	}

	protected Integer getInteger(String name, int def) {
		return getNumber(name, def).intValue();
	}

	protected Boolean getBoolean(String name, boolean def) {
		if (getOption(name) == null)
			return def;
		else
			return (Boolean) getOption(name);
	}
	
	public static String format(String target,Object...params){
		target = replaceEmpty(target);
		for (int i = 0; i < params.length; i++) {			
			params[i] = replaceEmpty(params[i]).replace("\\$", "\\\\\\$");
			target = target.replace("{" + i + "}",replaceEmpty(params[i]));
		}
		return target;
	}
	
	public static String escape(Object value) {
		return Json.escape(value);
	}
	
	public static String createRandom(int n){		
		String r = (int)(Math.random()*Math.pow(10,n)) + "";
		StringBuffer sbf = new StringBuffer(r);
		for(int i = 0;i < n - r.length();i++){
			sbf.insert(0,0);
		}
		return sbf.toString();
	}
	
	protected String createId(){		
		return this.getClass().getSimpleName() + "_" + createRandom(6);
	}
	
	public static void setDebug(boolean debug){
		AbsBase.debug = debug;
		Script.debug = debug;
		Json.debug = debug;
	}

}
